/*
 * @Author: your name
 * @Date: 2021-11-12 21:33:25
 * @LastEditTime: 2021-11-17 09:22:57
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\serve\app\service\article.js
 */
/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-11-11 09:09:44
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */

const Service = require("egg").Service;
const timeFormart = require("../utils/timeFormart");
class PageService extends Service {
    // /*************管理端****************** */
    //获取文章数据 +分页 +搜索
    async getPage() {
            const { ctx, app } = this;
            const { pageSize, pageIndex, searchVal, pathUrl, state } = ctx.request.body;
            let res = await app.mysql.select("page");
            let total = res.length;
            //搜索框搜索
            if (searchVal) {
                res = res.filter((item) => {
                    if (
                        item.title.indexOf(searchVal) !== -1 ||
                        item.title.includes(searchVal)
                    ) {
                        return item;
                    }
                });
                total = res.length;
            }
            //状态搜索
            if (state !== null) {
                res = res.filter((item) => item.state * 1 === state * 1);
                total = res.length;
            }
            //路径搜索
            if (pathUrl) {
                res = res.filter((item) => {
                    if (
                        item.pathUrl.indexOf(pathUrl) !== -1 ||
                        item.pathUrl.includes(pathUrl)
                    ) {
                        return item;
                    }
                });
                total = res.length;
            }
            //时间倒序
            res.sort((a, b) => {
                return new Date(b.public_time) - new Date(a.public_time);
            });
            //分页
            res = res.slice((pageIndex - 1) * pageSize, pageSize * pageIndex);

            return { res, total };
        }
        //更改页面状态
    async changePageState() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            //找到原数据
            const res = await app.mysql.get("page", {
                id,
            });
            //判断状态
            if (res.state * 1 === 1) {
                //更新文章状态
                await app.mysql.update("page", { id, state: 0 });
            } else {
                //更新文章状态
                await app.mysql.update("page", {
                    id,
                    state: 1,
                    public_time: timeFormart(Date.now()),
                });
            }
            return "操作成功";
        }
        //删除
    async delPage() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            const res = await app.mysql.delete("page", {
                id,
            });
            return res;
        }
        //添加页面
    async addPage() {
            const { ctx, app } = this;
            const { title, content, state, pathUrl, order, cover } = ctx.request.body;
            const isHavePathUrl = await app.mysql.get("page", { pathUrl }); //路径去重判断
            if (isHavePathUrl) return "页面已存在";
            const res = await app.mysql.insert("page", {
                ...ctx.request.body,
                public_time: timeFormart(Date.now()),
            });
            return res;
        }
        //修改页面
    async editPage() {
            const { ctx, app } = this;
            const { id, title, content, state, pathUrl, order, cover } =
            ctx.request.body;
            const isHavePathUrl = await app.mysql.get("page", { pathUrl }); //路径去重判断
            //判断isHavePathUrl是否存在
            if (isHavePathUrl) {
                if (isHavePathUrl.id !== id) return "页面已存在"; //修改内容存在切不与自己相同时
            }
            const res = await app.mysql.update("page", {
                id,
                ...ctx.request.body,
                public_time: timeFormart(Date.now()),
            });
            return res;
        }
        //获取页面详情
    async getPageDetail() {
            const { ctx, app } = this;
            const { id } = ctx.params;
            const res = await app.mysql.get("page", { id });
            return res;
        }
        //批量删除
    async batchDelPage() {
            const { ctx, app } = this;
            const { batchList } = ctx.request.body;
            batchList.forEach((item) => {
                app.mysql.delete("page", {
                    id: item.id,
                });
            });
            return "操作成功";
        }
        //批量发布
    async batchPubPage() {
            const { ctx, app } = this;
            const { batchList } = ctx.request.body;
            batchList.forEach((item) => {
                app.mysql.update("page", {
                    id: item.id,
                    state: 1,
                    public_time: timeFormart(Date.now()),
                });
            });
            return "操作成功";
        }
        //批量下线
    async batchDftPage() {
        const { ctx, app } = this;
        const { batchList } = ctx.request.body;
        batchList.forEach((item) => {
            app.mysql.update("page", {
                id: item.id,
                state: 0,
            });
        });
        return "操作成功";
    }

    //*************用户端****************** */
    //回去上线页面数据
    async getPassPage() {
            const { ctx, app } = this;
            let result = [];
            const res = await app.mysql.select("page");
            res.forEach((item) => {
                if (item.state * 1) {
                    result.push(item);
                }
            });
            result.sort((a, b) => {
                return b.order * 1 - a.order * 1;
            });
            return result;
        }
        //获取详情
    async getDetailPage() {
        const { ctx, app } = this;
        const { path } = ctx.params;
        const res = await app.mysql.get("page", { pathUrl: path });
        return res;
    }
}
module.exports = PageService;